Thread: while (var1=='y' || var1=='Y') better way ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    while (var1=='y' || var1=='Y') better way ?

    Hi
    ANy better/condensed way ?
    while (var1=='y' || var1=='Y')

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    maybe
    while(toupper(var1) == 'Y')
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    I don't think the second solution is better, as the function
    call is more expensive than a byte comparison.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by root4 View Post
    I don't think the second solution is better, as the function
    call is more expensive than a byte comparison.
    Right. Because efficiency is of the utmost importance when detecting whether the user typed 'y'.

    That was sarcasm.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    The 'better way' could be interpreted as 'more efficient', despite _indeed_ I don't see
    what could be really improved here... nevermind.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it was asked about condenced way...

    if talked about optimizing we can use
    Code:
    while (var1=='y' | var1=='Y')
    form and profile it...

    but supposing the input is get from user - what the sence to optimize for speed?
    so the only reason to modify the code - make it more readable...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> That was sarcasm.
    Heh. Probably wasn't need to qualify that.

    I think you're just going to have to live with it. If you want to feel better about it you could wrap it in a preprocessor or inline function. The thing is that it only looks nasty and messy but it's probably the best you can do. toupper is also not a bad alternative if you want something that looks more elegant.

    You could just ask the user to use lower or upper letters.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by root4 View Post
    The 'better way' could be interpreted as 'more efficient', despite _indeed_ I don't see
    what could be really improved here... nevermind.
    If you insist, you can write it like this:
    Code:
    const char lowerDiff = 'a' - 'A';
    while ((var1 | lowerDiff) == 'y')
    It relies on the fact that upper and lower case differ by one bit in ASCII though.

    But if you worry about efficiency at this level you'll never get anything done.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Quote Originally Posted by twomers View Post
    You could just ask the user to use lower or upper letters.
    If the user can do something stupid, he WILL.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by NeonBlack View Post
    If the user can do something stupid, he WILL.
    Depends on the context. The C language itself is case-sensitive. This is something that users of the language simply have to deal with.

    Although I agree in principle that a simple yes/no choice should probably be case insensitive simply for convenience, there could certainly be legitimate reasons to require input to be a certain form.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM